﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    public static class Helper_DateTime
    {
        /// <summary>
        /// 根据出生日期获取年龄（周岁）
        /// </summary>
        /// <param name="birthday"></param>
        /// <returns></returns>
        public static int Get_Age(DateTime? birthday)
        {
            if (birthday == null)
            {
                return 0;
            }
            DateTime time = Convert.ToDateTime(birthday);
            DateTime now = DateTime.Now;
            int age = now.Year - time.Year;
            if (age < 1)//第一种0岁的情况：年份差小于1
            {
                return 0;
            }
            time = time.AddYears(age);
            if (now >= time)
            {
                return age;
            }
            else
            {
                return age - 1;
            }
        }
        /// <summary>
        /// 将Decimal类型的时间转换为DateTime
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DecimalToDateTime(Decimal str)
        {
            return str.ToString().Insert(4, "-").Insert(7, "-").Insert(10, " ").Insert(13, ":").Insert(16, ":");
        }
    }
}
